package Web.service.impl;

import Web.constants.SystemCanstants;
import Web.domain.ResponseResult;
import Web.domain.dto.CategoryListDto;
import Web.domain.entity.Article;
import Web.domain.entity.Category;
import Web.mapper.ArticleMapper;
import Web.mapper.CategoryMapper;
import Web.service.CategoryService;
import Web.utils.BeanCopyUtils;
import Web.vo.HotArticleVO;
import Web.vo.PageVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;


@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private ArticleMapper articleMapper;
    @Override
    //查询标签列表
    public ResponseResult<PageVo> pageCategoryList(CategoryListDto CategoryListDto) {

        //分页查询的条件。模糊+分页
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        //第二、三个参数是互相比较。第一个参数是判空，当用户没有查询条件时，就不去比较后面两个参数
        queryWrapper.like(StringUtils.hasText(CategoryListDto.getDescription()),Category::getDescription,CategoryListDto.getDescription());

        List<Category> nameList= categoryMapper.selectList(queryWrapper);


        //使用查询到的catagory_id进一步查询article_id
        LambdaQueryWrapper<Article> artqueryWrapper = new LambdaQueryWrapper<>();
        //查询的不能是私密图片。也就是Status字段不能是0
        artqueryWrapper.eq(Article::getStatus, SystemCanstants.ARTICLE_STATUS_NORMAL);
        List<Article> articles=articleMapper.selectList(artqueryWrapper);
        articles.clear();
        Long[] article=new Long[nameList.size()];
        for(int i = 0 ;i < nameList.size() ;++i){
            article[i]=nameList.get(i).getId();
        }
        artqueryWrapper.in(Article::getCategoryId, article);
        articles=articleMapper.selectList(artqueryWrapper);
        List<HotArticleVO> articleVos = BeanCopyUtils.copyBeanList(articles, HotArticleVO.class);


        return ResponseResult.okResult(articleVos);

    }
}